home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Games / Game Sample Code / ZAM 1.0a13 / UtilCode / GrafUtils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-16  |  2.5 KB  |  129 lines  |  [TEXT/KAHL]

  1. /*
  2. //    GrafUtils
  3. //
  4. //    Graphics utility routines
  5. //    By Brigham Stevens
  6. //    Apple Computer Developer Technical Support
  7. //    © 1992 Apple Computer, Inc.
  8. //
  9. */
  10.  
  11.  
  12. #include "GrafUtils.h"
  13. #include "CoreAssertion.h"
  14. #include "ZAMProtos.h"
  15.  
  16. #define MAX_SAVEPORT_NEST 100
  17.  
  18. /* if this assertion fails, bump up the maximum stack size */
  19. #define CHECK_STACK()  ASSERT((__GStack != 0) && (__GStack < MAX_SAVEPORT_NEST))
  20.  
  21.  
  22. static GDHandle     __savgd[MAX_SAVEPORT_NEST];
  23. static CGrafPtr     __savegw[MAX_SAVEPORT_NEST];
  24. static GWorldPtr __lockPort[MAX_SAVEPORT_NEST];
  25. static short     __GStack = 1;
  26.  
  27. /*
  28.     PreserveGraf - saves the current grafics environment
  29.     and sets the new one to the passed in GWorld,
  30.     which it locks the pixels on for you
  31.     Also returns the PixMapHandle
  32.     
  33.     RestoreGraf - restores the grafics environment saved with PreserveGraf
  34.     
  35.     These calls are nestable.
  36. */
  37.  
  38.  
  39.  
  40. PixMapHandle PreserveGraf(GWorldPtr newPort)
  41. {
  42.     PixMapHandle    result;
  43.     
  44.     CHECK_STACK();
  45.     
  46.     GetGWorld(&__savegw[__GStack],&__savgd[__GStack]);
  47.     SetGWorld(newPort,nil);
  48.     
  49.     result = GetGWorldPixMap(newPort);
  50.     LockPixels(result);
  51.     __lockPort[__GStack] = newPort;
  52.  
  53.     ++__GStack;        /* bump to empty cell */
  54.     
  55.     return result;    
  56. }
  57.  
  58. void RestoreGraf(void)
  59. {
  60.     
  61.     /* subtract down to most recently used entry */
  62.     --__GStack;    
  63.     
  64.     CHECK_STACK();
  65.  
  66.     SetGWorld(__savegw[__GStack],__savgd[__GStack]);
  67.     UnlockPixels(GetGWorldPixMap(__lockPort[__GStack]));
  68.  
  69. }
  70.  
  71.  
  72. /*
  73.     
  74.     Creates a GWorld from the picture passed
  75. */
  76.  
  77. GWorldPtr PictureToGWorld(PicHandle    pict, int gdepth)
  78. {
  79.  
  80.     WindowPtr        saveWindow;
  81.     GDHandle        saveDevice;
  82.  
  83.     Rect            gwFrame;
  84.     GWorldPtr         gw;    
  85.     short            err;
  86.     
  87.     // get size of picture and translate to origin of 0,0
  88.     gwFrame = (**pict).picFrame;
  89.     OffsetRect( &gwFrame, -gwFrame.left, -gwFrame.top );
  90.  
  91.     err = NewGWorld(&gw ,gdepth,&gwFrame,GetCTable(gdepth),nil,0);
  92.  
  93.     if(err == noErr) {
  94.         GetGWorld(&saveWindow,&saveDevice);
  95.         SetGWorld(gw,nil);
  96.         
  97.         if(LockPixels (GetGWorldPixMap (gw)))
  98.         {
  99.             EraseRect(&gwFrame);
  100.             DrawPicture(pict,&gwFrame);
  101.             UnlockPixels(GetGWorldPixMap(gw));
  102.         }
  103.     }
  104.  
  105.     SetGWorld(saveWindow,saveDevice);
  106.     return gw;
  107. }
  108.  
  109.  
  110. void MyUnionRect(Rect *r1, Rect *r2, Rect *dest)
  111. /*
  112.     If one of the passed rects is empty
  113.     this one will return the other rect,
  114.     instead of making a big damn one from 0,0
  115.     I should have re-written this for speed,
  116.     but this is alpha, and it was faster to do it this way
  117.     in terms of having something to show.  However, 
  118.     I would get around to re-writting this in a comercial 
  119.     product.
  120. */
  121. {
  122.     if(EmptyRect(r1)) {
  123.         *dest = *r2;
  124.     } else if(EmptyRect(r2)) {
  125.         *dest = *r1;
  126.     } else {
  127.         UnionRect(r1,r2,dest);
  128.     }
  129. }